package com.jingdun.sport.common.util;

import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.SingleMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.base.payload.APNPayload;
import com.gexin.rp.sdk.exceptions.RequestException;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.TransmissionTemplate;

import lombok.extern.slf4j.Slf4j;


/**
 * 个推推送工具类
 *
 * @author lxp
 * @date 2019 -09-16 10:56:04
 */
@Slf4j
public class GeTuiUtil {
    //定义常量, appId、appKey、masterSecret 采用本文档 "第二步 获取访问凭证 "中获得的应用配置
    private static String appId = "hvqCkiUUWZ6b9CBb7Itd58";
    private static String appKey = "qmcq3kzTOP8J3qy832qAH4";
    private static String masterSecret = "M5mhCzhpqc5eKKy9dvfKi8";
//	(具体appId、appKey、masterSecret，在个推官网 )

    /**
     * 单个用户android推送
     *
     * @param cid
     * @param msg
     * @return
     * @author lxp
     * @date 2019 -09-18 09:25:51
     */
    public static boolean sendMessageAndroid(String cid, String msg) {
        IGtPush push = new IGtPush(appKey, masterSecret);
        TransmissionTemplate template = new TransmissionTemplate();
        template.setAppId(appId);
        template.setAppkey(appKey);
        template.setTransmissionType(2);
        template.setTransmissionContent(msg);

        // STEP2：设置通知样式
        //Style0 style = new Style0();
        // 设置通知栏标题与内容
        //style.setTitle("系统消息");
        //style.setText(msg);
        // 设置
        //template.setStyle(style);


        SingleMessage message = new SingleMessage();
        message.setOffline(true);
        // 离线有效时间，单位为毫秒，可选
        message.setOfflineExpireTime(24 * 3600 * 1000);
        message.setData(template);
        // 可选，1为wifi，0为不限制网络环境。根据手机处于的网络情况，决定是否下发
        message.setPushNetWorkType(0);
        Target target = new Target();
        target.setAppId(appId);
        target.setClientId(cid);
        IPushResult ret = null;
        try {
            ret = push.pushMessageToSingle(message, target);
        } catch (RequestException e) {
            e.printStackTrace();
            ret = push.pushMessageToSingle(message, target, e.getRequestId());
        }
        if (ret != null && ret.getResponse() != null && ret.getResponse().containsKey("result")) {
            log.info(ret.getResponse().toString());
            if (ret.getResponse().get("result").toString().equals("ok") && ret.getResponse().containsKey("status")) {
                return true;
            }
        }
        return false;
    }

    /**
     * 单个用户ios推送
     *
     * @param
     * @return
     */
    public static String sendMessageIos(String cId, String msg) {
        IGtPush push = new IGtPush(appKey, masterSecret);

        TransmissionTemplate template = new TransmissionTemplate();
        template.setAppId(appId);
        template.setAppkey(appKey);
        template.setTransmissionContent(msg);
        // 透传消息设置，1为强制启动应用，客户端接收到消息后就会立即启动应用；2为等待应用启动
        template.setTransmissionType(1);
        //设置ios
        template.setAPNInfo(getAPNPayload(msg));

        SingleMessage message = new SingleMessage();
        message.setData(template);
        message.setOffline(true);
        //离线有效时间，单位为毫秒，可选
        message.setOfflineExpireTime(24 * 1000 * 3600);
        // 可选，1为wifi，0为不限制网络环境。根据手机处于的网络情况，决定是否下发
        message.setPushNetWorkType(0);

        Target target = new Target();
        target.setAppId(appId);
        target.setClientId(cId);
        IPushResult ret = null;
        try {
            ret = push.pushMessageToSingle(message, target);
        } catch (RequestException e) {
            e.printStackTrace();
            ret = push.pushMessageToSingle(message, target, e.getRequestId());
        }
        if (ret != null) {
            return ret.getResponse().toString();
        } else {
            log.error("【IOS】个推，服务器异常");
            return "";
        }
    }

    public static void main(String[] args) {
        GeTuiUtil.sendMessageAndroid("20eda0e6f156b6da8d91dec43b0a3053", "{\"count\":\"1\",\"message\":\"我要推送\"}");
    }

    private static APNPayload getAPNPayload(String msg) {
        String myMsg = "{\"count\":\"1\",\"message\":\"" + msg + "\"}";
        //封装透传内容
        Map<String, Object> map = (Map<String, Object>) JSONObject.parse(myMsg);

        APNPayload payload = new APNPayload();
        //在已有数字基础上加1显示，设置为-1时，在已有数字上减1显示，设置为数字时，显示指定数字(设置角标)
        payload.setAutoBadge(String.valueOf(map.get("count")));
        payload.setContentAvailable(1);

        //简单模式APNPayload.SimpleMsg
        //payload.setAlertMsg(new APNPayload.SimpleAlertMsg(msg));
        //字典模式使用下者
        String body = (String) map.get("message");
        payload.setAlertMsg(getDictionaryAlertMsg(body));
        //添加附加信息
        payload.addCustomMsg("message", msg);
        return payload;
    }

    private static APNPayload.DictionaryAlertMsg getDictionaryAlertMsg(String body) {
        APNPayload.DictionaryAlertMsg alertMsg = new APNPayload.DictionaryAlertMsg();
        // iOS8.2以上版本支持
        alertMsg.setTitle("系统消息");
        alertMsg.setBody(body);
        return alertMsg;
    }
}

